package com.huabo.system.oracle.mapper;

import com.hbfk.util.DateUtil;
import com.hbfk.util.PageInfo;
import com.huabo.system.oracle.entity.TblContractTypeof;
import com.huabo.system.oracle.entity.TblCourse;
import com.huabo.system.oracle.entity.TblVideoType;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;

public class TblCourseMapperSqlConifg {
	
	
	public String selectListByPageInfo(com.hbfk.util.PageInfo<TblCourse> pageInfo, BigDecimal orgid, String coursename1, String coursetype1) {
		StringBuffer sqlSb = new StringBuffer("SELECT * FROM (SELECT BUDGET.*,ROWNUM RN FROM (SELECT * from TBL_COURSE where 1=1 AND PARENTID IS NULL AND ORGID="+orgid);
		if(!StringUtils.isBlank(coursename1)){
			sqlSb.append(" AND COURSENAME LIKE '%"+coursename1.trim()+"%'");
		}
		if(!StringUtils.isBlank(coursetype1)){
			sqlSb.append(" AND COURSETYPE LIKE '%"+coursetype1.trim()+"%'");
		}
		sqlSb.append(" ORDER BY COURSEID DESC ) BUDGET WHERE rownum <= "+(pageInfo.getCurrentPage()*pageInfo.getPageSize())+" ) WHERE RN > "+pageInfo.getCurrentRecord());
		String sql = sqlSb.toString();
		return sql;
	}
	
	public String selectCountByPageInfo(PageInfo<TblCourse> pageInfo, BigDecimal orgid, String coursename1, String coursetype1) {
		String sqlCount="select count(*) from TBL_COURSE  WHERE 1=1 AND PARENTID IS NULL AND ORGID="+orgid;
		if(!StringUtils.isBlank(coursename1)){
			sqlCount+=" and COURSENAME like '%"+coursename1.trim()+"%'";
		}
		if(!StringUtils.isBlank(coursetype1)){
			sqlCount+=" and COURSETYPE like '%"+coursetype1.trim()+"%'";
		}
		sqlCount+=" order by COURSEID";
		return sqlCount;
	}

	public String updatetblCourse(TblCourse tblCourse) {
		StringBuffer sql = new StringBuffer("UPDATE TBL_COURSE SET COURSENAME = '"+tblCourse.getCoursename()+"'");

		if(tblCourse.getMemo() != null && !"".equals(tblCourse.getMemo())) {
			sql.append(" , MEMO = '"+tblCourse.getMemo()+"'");
		}
		if(tblCourse.getParentid() != null && !"".equals(tblCourse.getParentid())) {
			sql.append(" , PARENTID = '"+tblCourse.getParentid()+"'");
		}
		if(tblCourse.getPicurl() != null && !"".equals(tblCourse.getPicurl())) {
			sql.append(" , PICURL = '"+tblCourse.getPicurl()+"'");
		}
		if(tblCourse.getVideourl() != null && !"".equals(tblCourse.getVideourl())) {
			sql.append(" , VIDEORUL = '"+tblCourse.getVideourl()+"'");
		}
		if(tblCourse.getUserid() != null && !"".equals(tblCourse.getUserid())) {
			sql.append(" , USERID = '"+tblCourse.getUserid()+"'");
		}
		if(tblCourse.getOrgid() != null && !"".equals(tblCourse.getOrgid())) {
			sql.append(" , ORGID = '"+tblCourse.getOrgid()+"'");
		}
		if(tblCourse.getCreateDate() != null) {
			sql.append(" ,CREATEDATE = TO_DATE('"+ DateUtil.parseDate(tblCourse.getCreateDate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(tblCourse.getCoursetype() != null && !"".equals(tblCourse.getCoursetype())) {
			sql.append(" , COURSETYPE = '"+tblCourse.getCoursetype()+"'");
		}
		if(tblCourse.getCoursenumber() != null && !"".equals(tblCourse.getCoursenumber())) {
			sql.append(" , COURSENUMBER = '"+tblCourse.getCoursenumber()+"'");
		}
		if(tblCourse.getType() != null && !"".equals(tblCourse.getType())) {
			sql.append(" , TYPE = '"+tblCourse.getType()+"'");
		}
		if(tblCourse.getCourseware() != null && !"".equals(tblCourse.getCourseware())) {
			sql.append(" , COURSEWARE = '"+tblCourse.getCourseware()+"'");
		}
		if(tblCourse.getCoursewareurl() != null && !"".equals(tblCourse.getCoursewareurl())) {
			sql.append(" , COURSEWAREURL = '"+tblCourse.getCoursewareurl()+"'");
		}
		sql.append(" WHERE COURSEID = '"+tblCourse.getCourseid()+"'");
		return sql.toString();
	}

	public String savetblCourse(TblCourse tblCourse) {
		StringBuffer column = new StringBuffer("INSERT INTO TBL_COURSE (COURSEID");
		StringBuffer value = new StringBuffer(" VALUES (HIBERNATE_SEQUENCE.nextval");

		if(tblCourse.getCoursename() != null) {
			column.append(",COURSENAME");
			value.append(",'"+tblCourse.getCoursename()+"'");
		}
		if(tblCourse.getMemo() != null) {
			column.append(",MEMO");
			value.append(",'"+tblCourse.getMemo()+"'");
		}
		if(tblCourse.getParentid() != null) {
			column.append(",PARENTID");
			value.append(",'"+tblCourse.getParentid()+"'");
		}
		if(tblCourse.getPicurl() != null) {
			column.append(",PICURL");
			value.append(",'"+tblCourse.getPicurl()+"'");
		}
		if(tblCourse.getVideourl() != null) {
			column.append(",VIDEORUL");
			value.append(",'"+tblCourse.getVideourl()+"'");
		}
		if(tblCourse.getUserid() != null) {
			column.append(",USERID");
			value.append(",'"+tblCourse.getUserid()+"'");
		}
		if(tblCourse.getOrgid() != null) {
			column.append(",ORGID");
			value.append(",'"+tblCourse.getOrgid()+"'");
		}
		if(tblCourse.getCreateDate() != null) {
			column.append(",CREATEDATE");
			value.append(",TO_DATE('"+DateUtil.parseDate(tblCourse.getCreateDate(),"yyyy-MM-dd HH:mm:ss") +"', 'YYYY-MM-DD HH24:MI:SS')");
		}
		if(tblCourse.getCoursetype() != null) {
			column.append(",COURSETYPE");
			value.append(",'"+tblCourse.getCoursetype()+"'");
		}
		if(tblCourse.getCoursenumber() != null ) {
			column.append(",COURSENUMBER");
			value.append(",'"+tblCourse.getCoursenumber()+"'");
		}
		if(tblCourse.getType() != null) {
			column.append(",TYPE");
			value.append(",'"+tblCourse.getType()+"'");
		}
		if(tblCourse.getCourseware() != null) {
			column.append(",COURSEWARE");
			value.append(",'"+tblCourse.getCourseware()+"'");
		}
		if(tblCourse.getCoursewareurl() != null) {
			column.append(",COURSEWAREURL");
			value.append(",'"+tblCourse.getCoursewareurl()+"'");
		}
		column.append(")");
		value.append(")");
		String sql = column.toString()+value.toString();
		return sql;
	}





	

	
}
